package org.example.SolutionNew003;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class DoubleTree006 {
    public static void main(String[] args) {
        DoubleTree006 dt = new DoubleTree006();
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(2);
        root.right.right = new TreeNode(3);
        root.left.right = new TreeNode(3);
        System.out.println(dt.levelOrder(root).toString());
    }

    //广度
    public List<List<Integer>> levelOrder1(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        if (root == null) return res;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        int size=0;
        while (!queue.isEmpty()) {
            size=queue.size();
            List<Integer> list = new ArrayList<>();
            while(size>0){
                TreeNode node = queue.poll();
                list.add(node.val);
                if (node.left != null) queue.add(node.left);
                if (node.right != null) queue.add(node.right);
                size--;
            }
            res.add(list);
        }
        return res;
    }

    //深度
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        if (root == null) return res;
        dpGetList(root,res,1);
        return res;
    }

    public void dpGetList(TreeNode root, List<List<Integer>> res, int level) {
        if (root == null) return;
        if (res.size()<level){
            List<Integer> list = new ArrayList<>();
            res.add(list);
        }
        res.get(level-1).add(root.val);
        dpGetList(root.left,res,level+1);
        dpGetList(root.right,res,level+1);
    }
}
